package com.llkj.qingnianguoxue.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.MyActionsAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.PreviousActionBean;
import com.llkj.qingnianguoxue.home.ActionDetailActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.AgreementActivity;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

public class MyActionsActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvActions;
    private MyActionsAdapter actionAdapter;
    private List<PreviousActionBean.ActionBean> actionData;
    private int totalSize, mPage, mCurrentPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_zhi_bo);
        setTitle("我的活动", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);

        initView();
        initListener();

        loadWaitDialog();
        onRefresh();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvActions = (RecyclerView) findViewById(R.id.recyclerView);
        lvActions.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        actionData = new ArrayList<>();
        actionAdapter = new MyActionsAdapter(R.layout.item_previous_action, actionData);
        lvActions.setAdapter(actionAdapter);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        actionAdapter.setOnLoadMoreListener(this);
        lvActions.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                PreviousActionBean.ActionBean actionBean = actionData.get(i);
                if("1".equals(actionBean.getType())){// 类型 1 内部 2 外部
                    Intent intent = new Intent(MyActionsActivity.this, ActionDetailActivity.class);
                    intent.putExtra("from_class", 2);
                    intent.putExtra("id", actionBean.getId());
                    startActivity(intent);
                }else{
                    Bundle bundle = new Bundle();
                    bundle.putInt("from_class", 4);
                    bundle.putString(AgreementActivity.WEB_URL,actionBean.getLink());
                    startActivity(AgreementActivity.class, bundle);
                }
            }
        });
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getActionData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getActionData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, actionAdapter, null, lvActions, totalSize);
    }

    /**
     * 我的活动
     */
    private void getActionData() {
        HttpMethod.myActivity(this, this);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_USER_MY_ACTIVITY) {
            PreviousActionBean actionBean = GsonUtil.GsonToBean(data, PreviousActionBean.class);
            if (actionBean != null) {
                totalSize = Integer.parseInt(actionBean.getNum());
                if (mPage == 1) {
                    actionData.clear();
                    LoadDataHelper.refreshComplete(actionAdapter, actionData, refreshLayout, lvActions);
                } else {
                    actionAdapter.setEnableLoadMore(true);
                    actionAdapter.loadMoreComplete();
                }
                actionData.addAll(actionBean.getActivity());
                actionAdapter.notifyDataSetChanged();
            }
        }
    }
}
